﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class ProductGroupBiz
    {
        #region Properties

        private readonly IProductGroupDao _productGroupDao;

        #endregion

        #region Constructors

        public ProductGroupBiz()
        {
            _productGroupDao = new ProductGroupDao();
        }

        public ProductGroupBiz(IProductGroupDao productGroupDao)
        {
            _productGroupDao = productGroupDao;
        }

        #endregion

        #region Methods

        public ProductGroup GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _productGroupDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public ProductGroup CreateGroup(ProductGroup productGroup)
        {
            // validate inputs
            if (productGroup == null) return null;
            if (string.IsNullOrEmpty(productGroup.Code) || string.IsNullOrEmpty(productGroup.Name)) return null;

            try
            {
                return _productGroupDao.AddProductGroup(productGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _productGroupDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _productGroupDao.GetCurrentIdentity() + 1;
        }

        public void UpdateGroup(ProductGroup productGroup)
        {
            if (productGroup == null) return;
            try
            {
                _productGroupDao.UpdateProductGroup(productGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetAllProductGroupsWs()
        {
            try
            {
                var peopleList = _productGroupDao.GetAllProductGroups();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateGroupWs(ProductGroup productGroup)
        {
            // validate inputs
            if (productGroup == null) return null;
            if (string.IsNullOrEmpty(productGroup.Code) || string.IsNullOrEmpty(productGroup.Name)) return null;

            try
            {
                var addedProductGroup = _productGroupDao.AddProductGroup(productGroup);
                return new { Result = "OK", Record = addedProductGroup };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateGroupWs(ProductGroup productGroup)
        {
            if (productGroup == null) return null;
            try
            {
                _productGroupDao.UpdateProductGroup(productGroup);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteGroupWs(int productGroupId)
        {
            try
            {
                _productGroupDao.DeleteProductGroup(productGroupId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers

        #endregion
    }
}
